// pages/Index/activityList.js
const controller=require("../controllers/Controller").controller

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:"",
    pageNumber:1,
    lastPage:false,
    title:""
  },

  //获取店铺首页商圈活动分页
  shopActivityPage() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.shopId = app.globalData.shopId
    value.type = this.data.type
    value.pageSize = 10
    controller.shopActivityPage(value).then(res => {
      res.data.list.forEach(item=>{
        item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
        item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
      })
      this.setData({
        actList: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  loadMore(){
    if(this.data.lastPage==false){
      value.pageNumber = this.data.pageNumber+1
      value.shopId = app.globalData.shopId
      value.type = this.data.type
      value.pageSize = 10
      this.setData({
        pageNumber:value.pageNumber
      })
      controller.shopActivityPage(value).then(res => {
        res.data.list.forEach(item=>{
          item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
          item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
        })
        let list=this.data.actList
        let list2=res.data.list
        let list3=list.concat(list2)
        this.setData({
          actList: list3,
          lastPage: res.data.lastPage
        })
      })
    }
  },

  //跳转详情页
  toActDetail(e) {
    let type = e.currentTarget.dataset.type
    let id = e.currentTarget.dataset.id
    let shopGroupId = e.currentTarget.dataset.shopgroupid
    wx.navigateTo({
      url: '/pages/TradingArea/GiftBagDetail?id=' + id + "&shopGroupId=" + shopGroupId + "&type=" + type,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
    if(options.type==5){
      this.setData({
        title:"超值礼包"
      })
    }else if(options.type==4){
      this.setData({
        title:"抵扣券"
      })
    }else if(options.type==3){
      this.setData({
        title:"欢乐拼"
      })
    }else if(options.type==2){
      this.setData({
        title:"店铺爆款"
      })
    }
    if(options.num){
      this.setData({
        num:options.num
      })
      this.readNum()
    }
    this.shopActivityPage()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
  //修改已读
  readNum(){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    if(this.data.type==5){
      value.name="groupGift"
    }else if(this.data.type==4){
      value.name="groupLimit"
    }else if(this.data.type==3){
      value.name="groupPin"
    }else if(this.data.type==2){
      value.name="groupHot"
    }
    value.num=this.data.num
    controller.readNum(value).then(res=>{

    })
  }
})